package cn.com.sise.mapper;

import cn.com.sise.pojo.Freshman;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class FreshmanSqlProvider {

    public String findFreshmanByMultiConditionSQL(final Map<String,Object> param){
       return new SQL(){
           {
               SELECT("*");
               FROM("tb_freshman");
               if (param.get("ticketNumber") != null || param.get("ticketNumber")!=""){
                   WHERE ("ticketNumber=#{ticketNumber}");
               }
               if (param.get("idCard") != null || param.get("idCard")!=""){
                   WHERE ("id_card=#{idCard}");
               }
               if (param.get("stuNo") != null || param.get("stuNo")!=""){
                   WHERE ("stuNo=#{stuNo}");
               }
           }
       }.toString();
    }

    public String insertFreshmanSQL(final Freshman freshman){
        return new SQL(){
            {
                INSERT_INTO("tb_freshman");
                if(freshman.getTicketNumber()!=null && freshman.getTicketNumber()!=""){
                    VALUES("ticketNumber","#{ticketNumber}");
                }
                if(freshman.getIdCard()!=null && freshman.getIdCard()!=""){
                    VALUES("id_card","#{idCard}");
                }
                if(freshman.getName()!=null && freshman.getName()!=""){
                VALUES("name","#{name}");
                }
                if(freshman.getAge()!=0){
                    VALUES("age","#{age}");
                }
                if(freshman.getStuNo()!=null && freshman.getStuNo()!=""){
                    VALUES("stuNo","#{stuNo}");
                }
                if(freshman.getSex()==0 || freshman.getSex()==1){
                    VALUES("sex","#{sex}");
                }
                if(freshman.getBirthday()!=null){
                    VALUES("birthday","#{birthday}");
                }

            }
        }.toString();
    }

    public String updateFreshmanSQL(final Freshman freshman){
        return new SQL(){
            {
                UPDATE("tb_freshman");
                if(freshman.getIdCard()!=null && freshman.getIdCard()!=""){
                    SET("id_card=#{idCard}");
                }
                if(freshman.getName()!=null && freshman.getName()!=""){
                    SET("name=#{name}");
                }
                if(freshman.getAge()!=null&& freshman.getAge()!=0){
                    SET("age=#{age}");
                }
                if(freshman.getStuNo()!=null && freshman.getStuNo()!=""){
                   SET("stuNo=#{stuNo}");
                }
                if(freshman.getSex()!=null && freshman.getSex()==0){
                    SET("sex=#{sex}");
                }
                if(freshman.getBirthday()!=null){
                    SET("birthday=#{birthday}");
                }
                WHERE("ticketNumber=#{ticketNumber}");
            }
        }.toString();
    }

    public String deleteFreshmanSQL(String tickerNumber){
        return new SQL(){
            {
                DELETE_FROM("tb_freshman");
                WHERE("ticketNumber=#{ticketNumber}");
            }
        }.toString();
    }

}
